home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / srld.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  3.0 KB  |  128 lines

  1. /* Copyright (C) 1995, 1996, 1997, 1998 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: srld.c,v 1.2 2000/09/19 19:00:51 lpd Exp $ */
  20. /* RunLengthDecode filter */
  21. #include "stdio_.h"        /* includes std.h */
  22. #include "memory_.h"
  23. #include "strimpl.h"
  24. #include "srlx.h"
  25.  
  26. /* ------ RunLengthDecode ------ */
  27.  
  28. private_st_RLD_state();
  29.  
  30. /* Set defaults */
  31. private void
  32. s_RLD_set_defaults(stream_state * st)
  33. {
  34.     stream_RLD_state *const ss = (stream_RLD_state *) st;
  35.  
  36.     s_RLD_set_defaults_inline(ss);
  37. }
  38.  
  39. /* Initialize */
  40. private int
  41. s_RLD_init(stream_state * st)
  42. {
  43.     stream_RLD_state *const ss = (stream_RLD_state *) st;
  44.  
  45.     return s_RLD_init_inline(ss);
  46. }
  47.  
  48. /* Refill the buffer */
  49. private int
  50. s_RLD_process(stream_state * st, stream_cursor_read * pr,
  51.           stream_cursor_write * pw, bool last)
  52. {
  53.     stream_RLD_state *const ss = (stream_RLD_state *) st;
  54.     register const byte *p = pr->ptr;
  55.     register byte *q = pw->ptr;
  56.     const byte *rlimit = pr->limit;
  57.     byte *wlimit = pw->limit;
  58.     int left;
  59.     int status = 0;
  60.  
  61. top:
  62.     if ((left = ss->copy_left) > 0) {
  63.     /*
  64.      * We suspended because the output buffer was full:;
  65.      * try again now.
  66.      */
  67.     uint avail = wlimit - q;
  68.     int copy_status = 1;
  69.  
  70.     if (left > avail)
  71.         left = avail;
  72.     if (ss->copy_data >= 0)
  73.         memset(q + 1, ss->copy_data, left);
  74.     else {
  75.         avail = rlimit - p;
  76.         if (left >= avail) {
  77.         copy_status = 0;
  78.         left = avail;
  79.         }
  80.         memcpy(q + 1, p + 1, left);
  81.         p += left;
  82.     }
  83.     q += left;
  84.     if ((ss->copy_left -= left) > 0) {
  85.         status = copy_status;
  86.         goto x;
  87.     }
  88.     }
  89.     while (p < rlimit) {
  90.     int b = *++p;
  91.  
  92.     if (b < 128) {
  93.         if (++b > rlimit - p || b > wlimit - q) {
  94.         ss->copy_left = b;
  95.         ss->copy_data = -1;
  96.         goto top;
  97.         }
  98.         memcpy(q + 1, p + 1, b);
  99.         p += b;
  100.         q += b;
  101.     } else if (b == 128) {    /* end of data */
  102.         if (ss->EndOfData) {
  103.         status = EOFC;
  104.         break;
  105.         }
  106.     } else if (p == rlimit) {
  107.         p--;
  108.         break;
  109.     } else if ((b = 257 - b) > wlimit - q) {
  110.         ss->copy_left = b;
  111.         ss->copy_data = *++p;
  112.         goto top;
  113.     } else {
  114.         memset(q + 1, *++p, b);
  115.         q += b;
  116.     }
  117.     }
  118. x:  pr->ptr = p;
  119.     pw->ptr = q;
  120.     return status;
  121. }
  122.  
  123. /* Stream template */
  124. const stream_template s_RLD_template = {
  125.     &st_RLD_state, s_RLD_init, s_RLD_process, 1, 1, NULL,
  126.     s_RLD_set_defaults
  127. };
  128.